package model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import database.Database;

public class Invitationlist {
	
	/*
	 * REGION private fields
	 */
	private int personID;
	private ArrayList<Meeting> meetings;
	private ArrayList<Meeting> pending;
	
	/*
	 * REGION public constructor
	 */
	public Invitationlist(int personID) {
		setID(personID);
		load();
	}
	
	/*
	 * REGION public getters
	 */
	public ArrayList<Meeting> getInvitationlist() {
		return meetings;
	}
	
	public ArrayList<Meeting> getPendingInvitations() {
		return pending;
	}
	
	public int getInvitationCount() {
		return meetings.size();
	}
	
	public int getPendingCount() {
		return pending.size();
	}
	
	/*
	 * REGION public setters
	 */
	public void setID(int personID) {
		this.personID = personID;
	}
	
	/*
	 * REGION public methods
	 */
	public void load() {
		
		// Establish a connection to the database
		Database database = new Database();
		database.connectToDatabase();
		
		try {
			ResultSet rs = database.getUserMeeting(personID);
			
			meetings = new ArrayList<Meeting>();
			pending = new ArrayList<Meeting>();
			
			while (rs.next()) {
				int meetingID = rs.getInt("meetingID");
				Meeting meeting = new Meeting(meetingID);
				meetings.add(meeting);
			}
			
			rs = database.getPendingInvitations(personID);
			
			while (rs.next()) {
				int meetingID = rs.getInt("meetingID");
				Meeting meeting = new Meeting(meetingID);
				meetings.add(meeting);
			}
		}
		catch(SQLException e) {
			e.printStackTrace();
		}	
		database.disconnectDatabase();
	}

}

